package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.*;
import com.itheima.mapper.*;
import com.itheima.service.db.QuestionnaireService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class QuestionnaireServiceImpl implements QuestionnaireService {

    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private SoulUserStatusMapper soulUserStatusMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;
    @Autowired
    private QuestionnaireResultMapper questionnaireResultMapper;

    @Autowired


    @Override
    public List<Questionnaire> findQuestionnaireAll() {
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(wrapper);
        return questionnaires;
    }

    @Override
    public SoulUserStatus findUserStatus(Long userId) {
        QueryWrapper<SoulUserStatus> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        SoulUserStatus soulUserStatus = soulUserStatusMapper.selectOne(wrapper);
        return soulUserStatus;
    }

    @Override
    public void insertUserStatus(SoulUserStatus soulUserStatus) {
        soulUserStatusMapper.insert(soulUserStatus);
    }

    @Override
    public List<SoulQuestion> findQuestion(Long questionnaireId) {
        QueryWrapper<SoulQuestion> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id", questionnaireId);
        List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(wrapper);
        return soulQuestionList;
    }

    @Override
    public List<SoulQuestionOption> findOption(Long questionId) {
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId);
        List<SoulQuestionOption> soulQuestionOptionList = soulQuestionOptionMapper.selectList(wrapper);
        return soulQuestionOptionList;
    }

    @Override
    public SoulQuestionOption findQuestionOption(String optionId) {
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("id", optionId);
        SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(wrapper);
        return soulQuestionOption;
    }

    @Override
    public QuestionnaireResult findResult(Integer questionnaireId, Integer presentScore) {
        QuestionnaireResult questionnaireResult = questionnaireResultMapper.findResult(questionnaireId, presentScore);
        return questionnaireResult;
    }


    @Override
    public SoulQuestion findQuestionById(String questionId) {
        QueryWrapper<SoulQuestion> wrapper = new QueryWrapper<>();
        wrapper.eq("id", questionId);
        SoulQuestion soulQuestion = soulQuestionMapper.selectOne(wrapper);
        return soulQuestion;
    }

    @Override
    public Questionnaire findQuestionnaire(Long questionnaireId) {
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        wrapper.eq("level", questionnaireId);
        Questionnaire questionnaire = questionnaireMapper.selectOne(wrapper);

        return questionnaire;
    }

    @Override
    public void updateLevel(SoulUserStatus userStatus) {
        soulUserStatusMapper.updateById(userStatus);
    }

    @Override
    public Integer findMaxLevel() {
        return questionnaireMapper.findMaxLevel();
    }

    @Override
    public Long findQuestionnaireByLevel(Integer newLevel) {
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        wrapper.eq("level", newLevel);
        Questionnaire questionnaire = questionnaireMapper.selectOne(wrapper);
        Long id = questionnaire.getId();
        return id;
    }

    @Override
    public PageBeanVo findQuestionnaireAllByPage(Integer pageNum, Integer pageSize) {
        // 1. 创建page对象
        IPage<Questionnaire> page = new Page<>(pageNum, pageSize);

        // 2. 创建查询条件
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();

        // 3. 执行查询
        page = questionnaireMapper.selectPage(page, wrapper);

        // 4. 返回数据
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

    @Override
    public PageBeanVo findQuestionListById(Integer id, Integer pageNum, Integer pageSize) {
        // 1. 创建page对象
        IPage<SoulQuestion> page = new Page<>(pageNum, pageSize);

        // 2. 创建查询条件
        QueryWrapper<SoulQuestion> wrapper = new QueryWrapper<>();

        wrapper.eq("questionnaire_id", id);
        // 3. 执行查询
        page = soulQuestionMapper.selectPage(page, wrapper);

        // 4. 返回数据
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

    // 删除问题
    @Override
    public void deleteQuestionById(Long id) {
        soulQuestionMapper.deleteById(id);
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", id);
        soulQuestionOptionMapper.delete(wrapper);
    }

    @Override
    public void saveQuestion(SoulQuestion soulQuestion, String[] option, Integer[] score, Integer[] extroversion, Integer[] judgement, Integer[] abstraction, Integer[] rationality) {
        // 添加题目
        soulQuestionMapper.insert(soulQuestion);

        // 添加问题的题目以及选项
        for (int i = 0; i < 4; i++) {
            SoulQuestionOption soulQuestionOption = new SoulQuestionOption();
            soulQuestionOption.setContent(option[i]); // 选项
            soulQuestionOption.setQuestionId(soulQuestion.getId()); // 选项所对应的问题id
            soulQuestionOption.setScore(score[i]);  // 分数
            soulQuestionOption.setExtroversion(extroversion[i]); // 外向
            soulQuestionOption.setJudgement(judgement[i]);  // 判断
            soulQuestionOption.setAbstraction(abstraction[i]); // 抽象
            soulQuestionOption.setRationality(rationality[i]); // 理性

            soulQuestionOptionMapper.insert(soulQuestionOption);
        }
    }



    // 添加问题的题目以及选项
    // public void saveQuestion(SoulQuestion soulQuestion, String optionA, String optionB, String scoreA, String scoreB, String optionC, String optionD, String scoreC, String scoreD) {
    //     // 添加题目
    //     soulQuestionMapper.insert(soulQuestion);
    //
    //     // 添加问题选项答案以及分数
    //     SoulQuestionOption soulQuestionOption1 = new SoulQuestionOption();
    //     soulQuestionOption1.setContent(optionA);
    //     soulQuestionOption1.setQuestionId(soulQuestion.getId());
    //     soulQuestionOption1.setScore(Integer.parseInt(scoreA));
    //
    //     SoulQuestionOption soulQuestionOption2 = new SoulQuestionOption();
    //     soulQuestionOption2.setContent(optionB);
    //     soulQuestionOption2.setQuestionId(soulQuestion.getId());
    //     soulQuestionOption2.setScore(Integer.parseInt(scoreB));
    //
    //     SoulQuestionOption soulQuestionOption3 = new SoulQuestionOption();
    //     soulQuestionOption2.setContent(optionC);
    //     soulQuestionOption2.setQuestionId(soulQuestion.getId());
    //     soulQuestionOption2.setScore(Integer.parseInt(scoreC));
    //
    //     SoulQuestionOption soulQuestionOption4 = new SoulQuestionOption();
    //     soulQuestionOption2.setContent(optionD);
    //     soulQuestionOption2.setQuestionId(soulQuestion.getId());
    //     soulQuestionOption2.setScore(Integer.parseInt(scoreD));
    //
    //     soulQuestionOptionMapper.insert(soulQuestionOption1);
    //     soulQuestionOptionMapper.insert(soulQuestionOption2);
    //     soulQuestionOptionMapper.insert(soulQuestionOption3);
    //     soulQuestionOptionMapper.insert(soulQuestionOption4);
    // }


    // 查询所有该questionId所对应的选项
    @Override
    public PageBeanVo findQuestionOptionById(Integer questionId, Integer pageNum, Integer pageSize) {

        // 1. 创建page对象
        IPage<SoulQuestionOption> page = new Page<>(pageNum, pageSize);

        // 2. 构造查询条件
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId);

        // 3. 执行查询
        page = soulQuestionOptionMapper.selectPage(page, wrapper);
        // 4. 返回数据
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

    // 问题选项数据回显
    @Override
    public SoulQuestionOption findQuestionOptionInfoById(Long questionQptionId) {
        return soulQuestionOptionMapper.selectById(questionQptionId);
    }

    @Override
    public void saveQuestionOptionInfo(Long id, String content, Integer score) {
        SoulQuestionOption soulQuestionOption = new SoulQuestionOption();
        soulQuestionOption.setId(id);
        soulQuestionOption.setContent(content);
        soulQuestionOption.setScore(score);
        soulQuestionOptionMapper.updateById(soulQuestionOption);
    }

    // 删除问题的选项
    @Override
    public void deleteQuestionOptionById(Long questionOptionId) {
        soulQuestionOptionMapper.deleteById(questionOptionId);
    }

    // 添加问题的选项
    @Override
    public void saveQuestionOption(SoulQuestionOption soulQuestionOption) {
        soulQuestionOptionMapper.insert(soulQuestionOption);
    }


}
